package com.think.model;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/30 0030.
 */
public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    public static User create(User user) {
        return new User(user.getId(),user.getUsername(),user.getPassword(),mapToGrantedAuthorities(user.getRoles()));
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<Role> authorities) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        for (Role role : authorities) {
           grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
        }
//        return authorities.stream().map(SimpleGrantedAuthority::new) .collect(Collectors.toList());
        return grantedAuthorities;
    }
}
